using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Calendar
{
    public partial class frmCalendar : Form
    {
        DataTable dt = new DataTable();
        int CurrMonth = 0, CurrYear = 0;

        public frmCalendar()
        {
            InitializeComponent();
        }

        private void frmCalendar_Load(object sender, EventArgs e)
        {
            DayOfWeek dow = DateTime.Now.DayOfWeek;
            Console.WriteLine( dow.ToString()); //Monday
            //Day.Monday 
            Console.WriteLine(DateTime.DaysInMonth(2010, 4));
            DateTime.IsLeapYear (2010); // true / false
            //DateTime.Today;  // current date
            DateTime.Today.AddDays(1);
            Console.WriteLine(DateTime.Today.Month.ToString());
            //DateTime.Parse("04/01/2010",


            //btnTest_Click(sender, e);
            UpdateCalender();
            //Icon_02.ico
            //notifyIcon1.Icon = new System.Drawing.Icon(System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal)  + @"\Icon.ico");
            notifyIcon1.Icon =   new System.Drawing.Icon(Application.StartupPath+"\\Icon_02.ico");
            notifyIcon1.Visible = true;
            notifyIcon1.Text = "SMART PM Calendar";

            this.Visible = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
            //ContextMenu mnuContextMenu = new ContextMenu();
            //this.ContextMenu = mnuContextMenu;
        }

        void init()
        {
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("SU"));
            dt.Columns.Add(new DataColumn("MO"));
            dt.Columns.Add(new DataColumn("TU"));
            dt.Columns.Add(new DataColumn("WD"));
            dt.Columns.Add(new DataColumn("TH"));
            dt.Columns.Add(new DataColumn("FR"));
            dt.Columns.Add(new DataColumn("SA"));
            DataRow r = dt.NewRow();
            dt.Rows.Add(r);
            DataRow r2 = dt.NewRow();
            dt.Rows.Add(r2);
            DataRow r3 = dt.NewRow();
            dt.Rows.Add(r3);
            DataRow r4 = dt.NewRow();
            dt.Rows.Add(r4);
            DataRow r5 = dt.NewRow();
            dt.Rows.Add(r5);
            DataRow r6 = dt.NewRow();
            dt.Rows.Add(r6);


            dgvCalendar.DataSource = dt;
            for (int i = 0; i < 7; i++)
            {
                dgvCalendar.Columns[i].Width = 50;
                dgvCalendar.Columns[i].ReadOnly  = true;
            }
            
        }

        void UpdateCalender() // Uses global vars CurrMonth and CurrYear
        {
            init();
            ///////////
            DateTime d;
            DayOfWeek dow = DayOfWeek.Friday;
            int firstDayCol = 0;
            if (CurrMonth == 0) CurrMonth = DateTime.Today.Month;
            if (CurrYear == 0) CurrYear = DateTime.Today.Year ;

            //d = DateTime.Parse(DateTime.Today.Month.ToString() + "/1/" + DateTime.Today.Year.ToString());
            d = DateTime.Parse(CurrMonth.ToString() + "/1/" + CurrYear.ToString());
            dow = d.DayOfWeek;

            if (dow.Equals(DayOfWeek.Sunday))
                firstDayCol = 0;
            else if (dow.Equals(DayOfWeek.Monday))
                firstDayCol = 1;
            else if (dow.Equals(DayOfWeek.Tuesday))
                firstDayCol = 2;
            else if (dow.Equals(DayOfWeek.Wednesday))
                firstDayCol = 3;
            else if (dow.Equals(DayOfWeek.Thursday))
                firstDayCol = 4;
            else if (dow.Equals(DayOfWeek.Friday))
                firstDayCol = 5;
            else if (dow.Equals(DayOfWeek.Saturday))
                firstDayCol = 6;
            ///////////

            int rowIdx = 0;
            for (int day = 1; day < DateTime.DaysInMonth(CurrYear, CurrMonth) + 1; day++)
            {
                d = DateTime.Parse(CurrMonth.ToString() + "/" + day.ToString() + "/" + CurrYear.ToString());
                dow = d.DayOfWeek;
                Console.WriteLine(day + ":: " + dow.ToString());

                if (((firstDayCol + day) % 7) == 0)
                    rowIdx = (firstDayCol + day) / 7 - 1;
                else
                    rowIdx = (firstDayCol + day) / 7;

                if (dow.Equals(DayOfWeek.Sunday))
                    dt.Rows[rowIdx]["SU"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Monday))
                    dt.Rows[rowIdx]["MO"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Tuesday))
                    dt.Rows[rowIdx]["TU"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Wednesday))
                    dt.Rows[rowIdx]["WD"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Thursday))
                    dt.Rows[rowIdx]["TH"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Friday))
                    dt.Rows[rowIdx]["FR"] = day.ToString();
                else if (dow.Equals(DayOfWeek.Saturday))
                    dt.Rows[rowIdx]["SA"] = day.ToString();
            }
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor = Color.Red;
            dgvCalendar.Rows[1].Cells["SU"].ToolTipText = "End date of 'Manage Project'";
            dgvCalendar.Rows[1].Cells["SU"].Style = style;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("SU"));
            dt.Columns.Add(new DataColumn("MO"));
            dt.Columns.Add(new DataColumn("TU"));
            dt.Columns.Add(new DataColumn("WD"));
            dt.Columns.Add(new DataColumn("TH"));
            dt.Columns.Add(new DataColumn("FR"));
            dt.Columns.Add(new DataColumn("SA"));
            DataRow r = dt.NewRow();
            dt.Rows.Add(r);
            DataRow r2 = dt.NewRow();
            dt.Rows.Add(r2);
            DataRow r3 = dt.NewRow();
            dt.Rows.Add(r3);
            DataRow r4 = dt.NewRow();
            dt.Rows.Add(r4);
            DataRow r5 = dt.NewRow();
            dt.Rows.Add(r5);
            DataRow r6 = dt.NewRow();
            dt.Rows.Add(r6);


            dgvCalendar.DataSource = dt;
            for (int i=0; i < 7; i++)
            dgvCalendar.Columns[i].Width = 60;

            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor=Color.Red;
            dgvCalendar.Rows[1].Cells["SU"].ToolTipText = "End date of 'Manage Project'";
            dgvCalendar.Rows[1].Cells["SU"].Style = style;

            ///////////
            DateTime d;
            DayOfWeek dow=DayOfWeek.Friday   ;
            int firstDayCol = 0;
            d = DateTime.Parse(DateTime.Today.Month.ToString() + "/1/" + DateTime.Today.Year.ToString());
            dow = d.DayOfWeek;

            if (dow.Equals(DayOfWeek.Sunday))
                firstDayCol = 0;
            else if (dow.Equals(DayOfWeek.Monday))
                firstDayCol = 1;
            else if (dow.Equals(DayOfWeek.Tuesday))
                firstDayCol = 2;
            else if (dow.Equals(DayOfWeek.Wednesday))
                firstDayCol = 3;
            else if (dow.Equals(DayOfWeek.Thursday))
                firstDayCol = 4;
            else if (dow.Equals(DayOfWeek.Friday))
                firstDayCol = 5;
            else if (dow.Equals(DayOfWeek.Saturday ))
                firstDayCol = 6;
            ///////////

            int rowIdx = 0;
            for (int day = 1; day < DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month) + 1; day++)
            {
                d = DateTime.Parse(DateTime.Today.Month.ToString() + "/" + day.ToString() + "/" + DateTime.Today.Year.ToString());
                dow = d.DayOfWeek;
                Console.WriteLine(day + ":: " + dow.ToString());

                if (((firstDayCol + day) % 7) ==0)
                    rowIdx = (firstDayCol + day) / 7 -1;
                else 
                rowIdx = (firstDayCol + day) / 7;

                if (dow.Equals( DayOfWeek.Sunday))
                    dt.Rows[rowIdx]["SU"] = day.ToString();
                else if (dow.Equals(  DayOfWeek.Monday))
                    dt.Rows[rowIdx]["MO"] = day.ToString();
                else if (dow.Equals( DayOfWeek.Tuesday))
                    dt.Rows[rowIdx]["TU"] = day.ToString();
                else if (dow.Equals(  DayOfWeek.Wednesday))
                    dt.Rows[rowIdx]["WD"] = day.ToString();
                else if (dow.Equals( DayOfWeek.Thursday))
                    dt.Rows[rowIdx]["TH"] = day.ToString();
                else if (dow.Equals(  DayOfWeek.Friday))
                    dt.Rows[rowIdx]["FR"] = day.ToString();
                else if (dow.Equals(  DayOfWeek.Saturday ))
                    dt.Rows[rowIdx]["SA"] = day.ToString();
            }

        }

        private void btnPrevYear_Click(object sender, EventArgs e)
        {
            CurrYear = CurrYear - 1;
            txtYear.Text = CurrYear.ToString();
            UpdateCalender();
        }

        private void btnNextYear_Click(object sender, EventArgs e)
        {
            CurrYear = CurrYear + 1;
            txtYear.Text = CurrYear.ToString();
            UpdateCalender();
        }

        private void btnNextMonth_Click(object sender, EventArgs e)
        {
            if (CurrMonth <12) CurrMonth = CurrMonth + 1;
            else if (CurrMonth == 12)
            {
                CurrMonth = 1;
                CurrYear = CurrYear + 1;
                txtYear.Text = CurrYear.ToString();
            }

            cboMonth.SelectedIndex = CurrMonth - 1;
            UpdateCalender();
        }

        private void btnPrevMonth_Click(object sender, EventArgs e)
        {
            if (CurrMonth > 1) CurrMonth = CurrMonth - 1;
            else if (CurrMonth == 0)
            {
                CurrMonth = 12;
                CurrYear = CurrYear - 1;
                txtYear.Text = CurrYear.ToString();
            }
            cboMonth.SelectedIndex = CurrMonth - 1;
            UpdateCalender();
        }

        private void calendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
             
            this.Visible = true;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Normal  ;
            //this.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }
    }
}